package com.key.win.business.chat.ctrl;

import com.key.win.base.auth.AuthenticationUtil;
import com.key.win.base.web.PageRequest;
import com.key.win.base.web.PageResult;
import com.key.win.base.web.Result;
import com.key.win.business.chat.model.ImGroupUser;
import com.key.win.business.chat.service.ImGroupUserService;
import com.key.win.common.log.annotation.LogAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@RequestMapping("/groupUser/*")
@Api("好友相关的api")
public class ImGroupUserCtrl {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ImGroupUserService imGroupUserService;

    @PostMapping("/findGroupUserByPaged")
    @ApiOperation(value = "GroupUser分页")
    @LogAnnotation(module = "im", recordRequestParam = false)
    public PageResult<ImGroupUser> findGroupUserByPaged(@RequestBody PageRequest<ImGroupUser> pageRequest) {
        return imGroupUserService.findGroupUserByPaged(pageRequest);
    }

    @GetMapping("/get/{id}")
    @ApiOperation(value = "获取GroupUser")
    @LogAnnotation(module = "im", recordRequestParam = false)
    public Result get(@PathVariable String id) {
        return Result.succeed(imGroupUserService.getById(id));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    @LogAnnotation(module = "im", recordRequestParam = true)
    public Result delete(@PathVariable String id) {
        boolean b = imGroupUserService.removeById(id);
        return Result.result(b);
    }

    @DeleteMapping("/deletes")
    @ApiOperation(value = "删除")
    @LogAnnotation(module = "im", recordRequestParam = true)
    public Result delete(@RequestBody Set<String> userIds) {
        return Result.result(imGroupUserService.batchDelete(userIds));
    }

    @PostMapping("/addGroupUser")
    @ApiOperation(value = "添加好友")
    @LogAnnotation(module = "im", recordRequestParam = true)
    public Result addGroupUser(@RequestBody ImGroupUser imGroupUser) {
        boolean b = imGroupUserService.addGroupUser(imGroupUser);
        return Result.result(b);
    }

    @PostMapping("/updateGroupUser")
    @ApiOperation(value = "更新好友")
    @LogAnnotation(module = "im", recordRequestParam = true)
    public Result updateGroupUser(@RequestBody ImGroupUser imGroupUser) {
        boolean b = imGroupUserService.updateGroupUser(imGroupUser);
        return Result.result(b);
    }

    @PostMapping("/addGroupUser/{groupId}")
    @ApiOperation(value = "创建组")
    @LogAnnotation(module = "im", recordRequestParam = true)
    public Result addUser(@RequestBody Set<String> userIds, @PathVariable String groupId) {
        ImGroupUser imGroupUser = new ImGroupUser();
        imGroupUser.setUserIds(userIds);
        imGroupUser.setGroupId(groupId);
        boolean b = imGroupUserService.batchAddGroupUser(imGroupUser);
        return Result.result(b);
    }

    @GetMapping("/findGroupUser/{groupId}")
    @ApiOperation(value = "根据组Id获取GroupUser")
    @LogAnnotation(module = "im", recordRequestParam = false)
    public Result getGroupUser(@PathVariable String groupId) {
        return Result.succeed(imGroupUserService.findGroupUserByGroupId(groupId));
    }
}
